<?php
/**
 * [模型]交易报表(淘宝数据)
 * 
 * @version $Id: Report.php 277 2012-04-24 08:37:00Z linsir123 $
 */
class adminReport
{
	/**
	 * 抓取
	 */
	public function fetch($startDate, $endDate)
	{
		echo '<style type="text/css">body{font-size:12px;color:#333}</style>';
		while ($startDate <= $endDate) {
			echo "<h4>".date('Y-m-d', $startDate)."</h4>";
			ob_flush();
			flush();
			
			///
			$date = date('Ymd', $startDate);
			$this->_fetchDay($date);
			$startDate += 3600*24;
		}
	}
	
	/**
	 * 取得报告列表
	 * // 取出当天的所有
	 */
	private function _fetchDay($date)
	{
		$list = array();
		
		$p = 1;
		$show = 20;
		$totalPage = 1;
		while ($totalPage > 0) {
			$data = $this->_getList($date, $p, $show);
			$tmp = (array) $data['taobaoke_report']['taobaoke_report_members']['taobaoke_report_member'];
			$list = array_merge($list, $tmp);
			if ($p == 1) {
				$total = (int) $data['taobaoke_report']['total_results'];
				$totalPage = ceil($total/$show);
			}
			
			///
			$p++;
			$totalPage--;
		}
		
		$this->_insert($list);
	}
	
	/**
	 * 取得报告列表
	 * // 通过`total_results`确认是否拉取结束
	 */
	private function _getList($date, $p = 1, $show = 20)
	{
		$req = M::topRequest();
		return $req->reportGet(array('date' => $date, 
			'p' => $p, 'show' => $show));
	}
	
	/**
	 * 插入交易报告列表
	 * // *、将记录插入到订单表
	 * // *、更新账户余额
	 */
	private function _insert($list)
	{
		$rate = App::O('taobaoke.rate');
		
		///
		$users = array();
		$pattern = "/^e35go([\d]+)$/";
		foreach ($list as $v) {
			$commission = round($v['commission']*$rate, 2);
			if ( ! isset($v['outer_code'])) {
				$this->_log($v);
				continue;
			}
			
			///
			preg_match($pattern, $v['outer_code'], $m);
			$uId = isset($m[1]) ? intval($m[1]) : 0;
			if (empty($uId)) {
				$this->_log($v);
				continue;
			}
				
			/* 订单 */
			$db = D::order();
			if ( ! $db->existsBy('order_id', $v['trade_id'])) {
				if ($this->_insertOrder($uId, $commission, $v)) {
					if (isset($users[$uId])) 
						$users[$uId] += $commission;
					else 
						$users[$uId] = $commission;
						
					$this->_log($v, "成功");
				} else 
					$this->_log($v, "订单插入异常");
			} else 
				$this->_log($v, "订单已存在");
		}
		
		/* 用户余额 */
		$db = D::account();
		foreach ($users as $uId => $money) 
			$db->in($uId, $money);
	}
	
	/**
	 * 插入订单
	 */
	private function _insertOrder($uId, $commission, $params)
	{
		$fields = array(
			'order_id' => 'trade_id',
			'num_iid' => 'num_iid',
			'seller_nick' => 'seller_nick',
			'item_title' => 'item_title',
			'item_num' => 'item_num',
			'price' => 'pay_price',
			'create_time' => 'pay_time',
		);
		
		///
		$data = array('uid'=>$uId, 'commission'=>$commission);
		foreach ($fields as $k => $v) 
			$data[$k] = trim($params[$v]);
			
		return D::order()->insert($data);
	}
	
	/**
	 * 日志
	 */
	private function _log($v, $message = "无返现代码")
	{
		echo '<div>',$message,'&nbsp||&nbsp',
			$v['trade_id'],'&nbsp||&nbsp',
			$v['commission'],'&nbsp||&nbsp',
			$v['pay_price'],'&nbsp||&nbsp',
			$v['item_title'],
			'</div>';
		echo '<script type="text/javascript">top.loadFix();</script>';
		ob_flush();
		flush();
	}
}
